<!DOCTYPE html>
<!--
Copyright (c) 2016 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/core/test_utils.html">
<link rel="import" href="/tracing/extras/importer/linux_perf/ftrace_importer.html">

<script>
'use strict';

tr.b.unittest.testSuite(function() {
  test('i2cImport', function() {
    const lines = [

      ' /system/bin/surfaceflinger-516   (  516) [002] ...1  6481.291425: ' +
        'i2c_write: i2c-3 #0 a=020 f=0000 l=2 [15-00]',
      ' /system/bin/surfaceflinger-516   (  516) [002] ...1  6481.291602: ' +
        'i2c_result: i2c-3 n=1 ret=1',
      ' kworker/u8:5-9309  ( 9309) [001] ...1  6481.365821: ' +
        'i2c_write: i2c-7 #0 a=01d f=0000 l=1 [00]',
      ' kworker/u8:5-9309  ( 9309) [001] ...1  6481.365823: ' +
        'i2c_read: i2c-7 #1 a=01d f=0001 l=1',
      ' kworker/u8:5-9309  ( 9309) [001] ...1  6481.366730: ' +
        'i2c_reply: i2c-7 #1 a=01d f=0001 l=1 [01]',
      ' kworker/u8:5-9309  ( 9309) [001] ...1  6481.366732: ' +
        'i2c_result: i2c-7 n=2 ret=2'
    ];
    const m = tr.c.TestUtils.newModelWithEvents([lines.join('\n')], {
      shiftWorldToZero: false
    });
    assert.isFalse(m.hasImportWarnings,
        `Expected no import warnings but got ${m.importWarnings}`);

    let threads = m.getAllThreads();
    assert.strictEqual(threads.length, 2);

    threads = m.findAllThreadsNamed('i2c adapter 3');
    assert.strictEqual(threads.length, 1);
    assert.strictEqual(threads[0].sliceGroup.length, 1);
    let slice = threads[0].sliceGroup.slices[0];
    assert.strictEqual(slice.title, 'i2c write');
    assert.isDefined(slice.args);
    assert.strictEqual(slice.args['Message number'], '0');
    assert.strictEqual(slice.args.Address, '020');
    assert.strictEqual(slice.args.Flags, '0000');
    assert.strictEqual(slice.args['Data Length'], '2');
    assert.strictEqual(slice.args.Data, '[15-00]');

    threads = m.findAllThreadsNamed('i2c adapter 7');
    assert.strictEqual(threads.length, 1);
    assert.strictEqual(threads[0].sliceGroup.length, 3);

    slice = threads[0].sliceGroup.slices[0];
    assert.strictEqual(slice.title, 'i2c write');
    assert.strictEqual(slice.args['Message number'], '0');
    assert.strictEqual(slice.args.Address, '01d');
    assert.strictEqual(slice.args.Flags, '0000');
    assert.strictEqual(slice.args['Data Length'], '1');
    assert.strictEqual(slice.args.Data, '[00]');

    slice = threads[0].sliceGroup.slices[1];
    assert.strictEqual(slice.title, 'i2c read');
    assert.strictEqual(slice.args['Message number'], '1');
    assert.strictEqual(slice.args.Address, '01d');
    assert.strictEqual(slice.args.Flags, '0001');
    assert.strictEqual(slice.args['Data Length'], '1');

    slice = threads[0].sliceGroup.slices[2];
    assert.strictEqual(slice.title, 'i2c reply');
    assert.strictEqual(slice.args['Message number'], '1');
    assert.strictEqual(slice.args.Address, '01d');
    assert.strictEqual(slice.args.Flags, '0001');
    assert.strictEqual(slice.args['Data Length'], '1');
    assert.strictEqual(slice.args.Data, '[01]');
    assert.strictEqual(slice.args['Number of messages'], '2');
    assert.strictEqual(slice.args.Return, '2');
  });
});
</script>
